<?php

namespace app\controller;

use Exception;
use think\facade\Db;
use think\facade\Request;


class Qq
{

    public function check_qq_in_black()
    {
        try {
            $data = Request::param();
            $qq = $data['qq'];
            #验证操作对象是否在black_qq中
            $r = Db::table('black_qq')->where('id', $qq)->find();
            if ($r) {
                if ($r['state'] == '0'){
                    return (returnjson(msg: 'qq号' . $qq . '曾被拉黑，于' .date("Y-m-d H:i:s",strtotime($r['last_time'])) . '被解封',data:['black'=>0]));
                }else{
                    log_other('QQ号' . $qq . '在黑名单中,试图访问'.$data['method_name'].'功能');
                    return (returnjson(msg: 'qq号' . $qq . '在黑名单中，于' . date("Y-m-d H:i:s",strtotime($r['last_time']))  . '被封禁',data:['black'=>1]));
                }
            }
            return (returnjson(msg: 'qq号' . $qq . '不在黑名单中',data:['black'=>0]));
        } catch (Exception $e) {
            return returnJson(500, "发生错误", $e->getMessage());
        }
    }
    // 封禁QQ
    public function black_add_qq()
    {
        try {
            $data = Request::param();
            $qq = $data['qq'];
            $operator = $data['operator'];
            $reason = $data["reason"];
            $array = ['id' => $qq, 'operator' => $operator, 'reason' => $reason,'state' => 1];
            $r = Db::table('black_qq')->where('id', $qq)->find();
            if ($r) {
                if ($r['state'] == '0') {
                    Db::table('black_qq')->where('id', $qq)->update($array);
                    return (returnjson(msg:'QQ号' . $qq . '曾于' .date("Y-m-d H:i:s",strtotime($r['last_time']))  . '被' . $r['operator'] . '解封，封禁成功'));
                }
                return (returnjson(msg:'QQ号' . $qq . '曾于' . date("Y-m-d H:i:s",strtotime($r['last_time'])) . '被' . $r['operator'] . '封禁，不需要再次封禁'));
            }
            Db::table('black_qq')->insert($array);
            return (returnjson(msg: '封禁QQ号' . $qq . '成功'));
        } catch (Exception $e) {
            return returnJson(500, "发生错误", $e->getMessage());
        }
    }
    //解封QQ
    public function black_delete_qq()
    {
        try {
            $data = Request::param();
            $qq = $data['qq'];
            $operator = $data['operator'];
            $reason = $data['reason'];
            $array = ['id' => $qq, 'operator' => $operator, 'reason' => $reason, 'state' => '0'];
            $r = Db::table('black_qq')->where('id', $qq)->find();
            if ($r == null or $r == [] or $r == '') return (returnjson(msg: 'QQ号' . $qq . '不在黑名单中，不需要解封'));
            if ($r['state'] == 0) return (returnjson(msg: 'QQ号' . $qq . '曾于' . date("Y-m-d H:i:s",strtotime($r['last_time']))  . '被' . $r['operator'] . '解封,不需要再次解封', data: ['old_data' => $r]));
            Db::table('black_qq')->where('id', $qq)->update($array);
            return (returnjson(msg: '解封QQ号' . ($qq) . '成功'));
        } catch (Exception $e) {
            return returnJson(500, "发生错误", $e->getMessage());
        }
    }








}